package com.example.demo.service;

import com.example.demo.mapper.BlogMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.Blog;
import com.example.demo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;
    public User selectUserByName(String userName) {
        return userMapper.getUserByName(userName);
    }

    /**
     * 根据博客ID获取作者信息
     * @param blogId
     * @return
     */
    public User getAuthorInfoByBlogId(Integer blogId) {
        //1.获取作者id
        Blog blog = blogMapper.selectById(blogId);
        if(blog == null || blog.getUserId() < 0){
            return null;
        }
        //2.根据作者id获取作者信息
        return userMapper.getUserById(blog.getUserId());
    }
}
